package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.ICompanyDao;
import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.util.GetSqlSession;
import com.cn.wanxi.util.JDBCUtils;
import redis.clients.jedis.Jedis;

import java.sql.ResultSet;
import java.sql.SQLException;

public class CompanyDaoImpl implements ICompanyDao {
    @Override
    public CompanyModel getCompany() {
        return GetSqlSession.getSqlSession().selectOne("com.cn.wanxi.dao.ICompanyDao.getCompany");
    }

    @Override
    public int editCompany(CompanyModel company) {
        try {
            Jedis jedis = new Jedis();
            jedis.del("company_id");
        }catch (Exception e){
            e.printStackTrace();
        }
        return GetSqlSession.getSqlSession().update("com.cn.wanxi.dao.ICompanyDao.editCompany",company);
    }

    @Override
    public CompanyModel getCompanyByRedis() {
        Jedis jedis = new Jedis();
        CompanyModel company = new CompanyModel();
        company.setId(Integer.valueOf(jedis.get("company_id")));
        company.setAd(jedis.get("ad"));
        company.setAddress(jedis.get("company_address"));
        company.setCopyright(jedis.get("copyright"));
        company.setCpy_intro(jedis.get("cpy_intro"));
        company.setEmail(jedis.get("company_email"));
        company.setEnglish_name(jedis.get("english_name"));
        company.setFax(jedis.get("fax"));
        company.setIntro(jedis.get("company_intro"));
        company.setLogo(jedis.get("logo"));
        company.setName(jedis.get("company_name"));
        company.setPhone(jedis.get("company_phone"));
        company.setWebsite(jedis.get("website"));
        company.setWork_time(jedis.get("work_time"));
        System.out.println("redis");
        return company;
    }

    @Override
    public void setCompanyInRedis(CompanyModel company) {
        Jedis jedis = new Jedis();
        jedis.set("company_id", String.valueOf(company.getId()));
        jedis.set("ad",company.getAd());
        jedis.set("company_address",company.getAddress());
        jedis.set("copyright",company.getCopyright());
        jedis.set("company_email",company.getEmail());
        jedis.set("english_name",company.getEnglish_name());
        jedis.set("fax",company.getFax());
        jedis.set("company_intro",company.getIntro());
        jedis.set("cpy_intro",company.getCpy_intro());
        jedis.set("logo",company.getLogo());
        jedis.set("company_name",company.getName());
        jedis.set("company_phone",company.getPhone());
        jedis.set("website",company.getWebsite());
        jedis.set("work_time",company.getWork_time());
    }
}
